//
//  PictorialViewController.m
//  SimplystylePictorial
//
//  Created by Mac on 15/7/7.
//  Copyright (c) 2015年 Hu. All rights reserved.
//

#import "PictorialViewController.h"
#import "Header.h"
#import "imageModel.h"

@interface PictorialViewController ()<MBProgressHUDDelegate,UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    MBProgressHUD *HUD;
    UITableView *_tableView;
}

@property (nonatomic,copy)NSMutableArray* itemArray;
@property (nonatomic,copy)NSMutableArray* infoArray;
@end

@implementation PictorialViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    __weak PictorialViewController* weakSelf = 
    [_tableView addPullToRefreshWithActionHandler:^{
        [self performSelector:@selector(downLoad) withObject:self afterDelay:1.2f];
        [self getInfoGetRequest];
    }];
}

- (void)downLoad
{
    [_tableView.pullToRefreshView stopAnimating];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.itemArray = [[NSMutableArray alloc]initWithCapacity:0];
    self.infoArray = [[NSMutableArray alloc]initWithCapacity:0];
    
    HUD = [[MBProgressHUD alloc]initWithView: self.navigationController.view];
    HUD.delegate = self ;
    HUD.labelText = @"玩命加载中...";
    [HUD show:YES];
    [self.navigationController.view addSubview:HUD];
    
    //调用方法 进行get请求
    [self getInfoGetRequest];
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self ;
    _tableView.dataSource = self ;
    _tableView.separatorStyle = NO;
    [self.view addSubview:_tableView];
}

- (void)getInfoGetRequest
{
    AFHTTPRequestOperationManager *request = [AFHTTPRequestOperationManager manager];
    [request GET:@"http://mmmono.com/api/category/image?page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.itemArray = responseObject[@"items"];
        NSMutableArray *data = [[NSMutableArray alloc]init];
        for(NSDictionary *dict in self.itemArray)
        {
            imageModel *model = [imageModel setInfoWithDict:dict];
            [data addObject:model];
        }
        self.infoArray = data;
        [_tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"sorry,网络发生错误" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alterView show];
    }];

}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.itemArray count];
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    imageModel *model = self.infoArray[indexPath.row];
    if(self.infoArray.count != 0)
    {
        [HUD hide:YES];
    }
    
    UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, kScreenW-10, (kScreenW-10)*3/5)];
    cellImageView.clipsToBounds = YES ;
    cellImageView.contentMode = UIViewContentModeScaleAspectFill ;
    [cellImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb_url]];
    [cell addSubview:cellImageView];
    
    UIImageView*cellImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, ((kScreenW-10)*3/5+15), 30, 50)];
    cellImage.image=[UIImage imageNamed:@"icon-pic"];
    cellImage.clipsToBounds=YES;
    [cell addSubview:cellImage];
    
    UILabel*titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, ((kScreenW-10)*3/5+10), kScreenW-50, 30)];
    titleLabel.text=model.title;
    titleLabel.font=[UIFont boldSystemFontOfSize:16];
    [cell addSubview:titleLabel];
    
    UILabel*textLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, ((kScreenW-10)*3/5+35), kScreenW-60, 60)];
    textLabel.text=model.text;
    textLabel.numberOfLines=0;
    textLabel.font=[UIFont boldSystemFontOfSize:13];
    [cell addSubview:textLabel];
    
    UILabel*authorLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, ((kScreenW-10)*3/5+85), 200, 30)];
    authorLabel.text=[NSString stringWithFormat:@"🍭%@/%@   %@",model.read_count,model.action_count,model.author];
    authorLabel.numberOfLines=0;
    authorLabel.font=[UIFont boldSystemFontOfSize:12];
    [cell addSubview:authorLabel];

    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kScreenW-10)*3/5+120;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;//表头高度
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [HUD hide:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictorialWebViewViewController *pictorialVC = [[PictorialWebViewViewController alloc]init];
    imageModel *model = self.infoArray[indexPath.row];
    pictorialVC.hidesBottomBarWhenPushed = YES ;
    pictorialVC.title = model.title;
    pictorialVC.webUrl = model.item_url;
    [self.navigationController pushViewController:pictorialVC animated:YES];
    
}
@end
